<?php
/** Copyright 2014 TinPhp Systems, Inc.
 * @author zhangxilian 2013-8-15
 * @package TinPhp
 */
if (!defined('IN_TINPHP')) {exit('Access Denied');}

require_once ("smarty/Smarty.class.php");
/** TinPHP模板类，继承Smarty */
class TinTemplate extends Smarty {
	/** 初始化模板类
     * @param bool $caching 是否启用Smarty缓存，默认false
     * @param int $cacheTime 缓存有效时间(秒),默认1200 */
	public function TinTemplate($caching = false, $cacheTime = 1200) {
		parent::__construct();
		$this->template_dir = TIN_APP_PATH . "templates/";
		$this->compile_dir = TIN_APP_PATH . "templates_c/";
		$this->config_dir = TIN_APP_PATH . "configs/";
		$this->cache_dir = TIN_APP_PATH . "cache/";
		
		$this->left_delimiter = "{{";
		$this->right_delimiter = "}}";
		if ($caching) {
			$this->caching = $caching;		
		}
		if (!is_null($cacheTime)) {
			$this->cache_lifetime = $cacheTime;
		}
	}
	/** 设置是否启用缓存属性
     * @param int $c 根据Smarty的caching属性值 */
    public function setCaching($c) {
		$this->caching = $c;
	}
    /** 设置缓存时间属性
     * @param int $ct 时间(秒) */
	public function setCacheTime($ct) {
		$this->cache_lifetime = $ct;
	}
    /** 重命名Smarty的设置值方法assign
     * @param string $name 变量名称
     * @param object $val 变量值 */
	public function set($name, $val = null) {
		$this->assign($name, $val);
	}
    /** 检测是否已经缓存该模板
     * @param string $tplName 缓存名称
     * @param string $cacheId 缓存的cacheid */
	public function isCached($tplName, $cacheId = NULL) {
		return $this->is_cached($tplName, $cacheId);
	}
//	public function registerFunction($tagName, $funcName) {
//		//$this->register_function($tagName, $funcName);
//		$this->registerPlugin("function", $tagName, $funcName, false);
//	}
//	public function registerBlock($tagName, $funcName = null) {
//		if ($funcName == null) {
//			//$this->register_block($tagName, "tin_default_dynamic_block_func", false);
//			$this->registerPlugin("block", $tagName, "tin_default_dynamic_block_func", false);
//		} else {
//			//$this->register_block($tagName, $funcName, false);
//			$this->registerPlugin("block", $tagName, $funcName, false);
//		}
//	}
    /** 获取当前Smarty输出的HTML
     * @param string $tplName 模板名称
     * @param object $cacheId 缓存ID */
	public function getOutput($tplName, $cacheId = NULL) {
		return $this->fetch($tplName, $cacheId); 
	}
    /** 输出Smarty最终的解析结果，展示页面
     * @param string $tplName 模板名称
     * @param object $cacheId 缓存ID */
	public function render($tplName, $cacheId = NULL) {
		$this->display($tplName, $cacheId);
	}
    /** 获取呈现Smarty模板，类似getOutput
     * @param string $tplName 模板名称
     * @param object $cacheId 缓存ID */
	public function displayStr($tplName, $cacheId = NULL) {
		return $this->fetch($tplName, $cacheId, NULL, false);
	}
} //End Class

?>
